package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.UserBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.HxUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.TimeCount;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class BindPhoneActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private TimeCount timeCount;
    private String phone;
    private String yzm;
    private String third_no;
    private String lg_status;
    private String headurl;
    private String nickname;

    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
        third_no=getIntent().getStringExtra("third_no");
        lg_status=getIntent().getStringExtra("lg_status");
        headurl=getIntent().getStringExtra("headurl");
        nickname=getIntent().getStringExtra("nickname");
        initView();
    }

    private void initView() {
//        timeCount=new TimeCount(tvGetYzm,2*60*1000,1000);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("绑定手机号");
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_get_yzm, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_get_yzm:
                phone=etName.getText().toString();
                if(TextUtils.isEmpty(phone)){
                    ToastUtils.toastForShort(this,"手机号码不能为空");
                    return;
                }
                getYzm();
                break;
            case R.id.tv_confirm:
                phone=etName.getText().toString();
                if(TextUtils.isEmpty(phone)){
                    ToastUtils.toastForShort(this,"手机号码不能为空");
                    return;
                }
                yzm=etYzm.getText().toString();
                if(TextUtils.isEmpty(yzm)){
                    ToastUtils.toastForShort(this,"验证码不能为空");
                    return;
                }
                confirm();
                break;
        }
    }

    private void confirm() {
        OkgoUtil.post(Http.BindUserPhone,this)
                .params("phone_no",phone)
                .params("codeno",yzm)
                .params("third_no",third_no)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response==null){return;}
                        String res=response.body().toString();
                        BaseBean bean= new Gson().fromJson(res, BaseBean.class);
                        if(bean==null){return;};
                        if(bean.getCode()== Constant.SUCESS){
                            loginUser();
                        }
                        ToastUtils.toastForShort(BindPhoneActivity.this,bean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(BindPhoneActivity.this,getString(R.string.bad_net));
                    }
                });

    }

    private void getYzm() {
        OkgoUtil.post(Http.YZM,this)
                .params("phone_no",phone)
                .params("status",4)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response==null){return;}
                        String res=response.body().toString();
                        BaseBean bean= new Gson().fromJson(res, BaseBean.class);
                        if(bean==null){return;};
                        if(bean.getCode()== Constant.SUCESS){
                            timeCount=new TimeCount(tvGetYzm,2*60*1000,1000,"#ffffff");
                            timeCount.start();
                        }
                        ToastUtils.toastForShort(BindPhoneActivity.this,bean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(BindPhoneActivity.this,getString(R.string.bad_net));
                    }
                });

    }

    private void loginUser() {
        loadingDialog.show();
        OkgoUtil.post(Http.LOGIN,this)
                .params("phone_no","")
                .params("password","")
                .params("lg_status",lg_status)
                .params("headurl",headurl)
                .params("nickname",nickname)
                .params("third_no",third_no)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        UserBean userBean= DealResult.getInstance().dealDatas(BindPhoneActivity.this,response,new TypeToken<UserBean>() {}.getType());
                        if(userBean==null){return;}
                        int code=userBean.getCode();
                         if(code==Constant.SUCESS){
                            UserBean.BodyBean bean = userBean.getBody();
                             HxUtils.login(bean.getUserid()+"");
                             Set<String> tags = new HashSet<>();
                             tags.add(bean.getUserid()+"");
                             JPushInterface.setTags(getApplicationContext(),1,tags);
                             SPUtils.put(BindPhoneActivity.this,Constant.USER_ID,bean.getUserid());
                            SPUtils.put(BindPhoneActivity.this,Constant.USER_Token,bean.getUser_token());
                            SPUtils.put(BindPhoneActivity.this,Constant.OPEN_ID,third_no);
                             SPUtils.put(BindPhoneActivity.this,Constant.HEAD_URL,bean.getHeadurl());
                             SPUtils.put(BindPhoneActivity.this,Constant.NICK_NAME,bean.getNickname());
                             SPUtils.put(BindPhoneActivity.this,Constant.ZONDIAC,bean.getZodiac());
                             SPUtils.put(BindPhoneActivity.this,Constant.PHONE,bean.getPhone_no());
                            if(bean.getPassword()==2){
                                BindPhoneActivity.this.finish();
                                Intent i=new Intent(BindPhoneActivity.this,SetPswActivity.class);
                                i.putExtra("flag",0);
                                startActivity(i);
                            }else if(bean.getSetsex()==2){
                                BindPhoneActivity.this.finish();
                                startActivity(new Intent(BindPhoneActivity.this,ChooseSexActivity.class));
                            }else if(bean.getIntent()==2){
                                BindPhoneActivity.this.finish();
                                startActivity(new Intent(BindPhoneActivity.this,SetLabel1211Activity.class));
                            }else{
                                BindPhoneActivity.this.finish();
                                toMain(bean);
                            }
                        }else{
                            ToastUtils.toastForShort(BindPhoneActivity.this,userBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(BindPhoneActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void toMain(UserBean.BodyBean bean) {
        SPUtils.put(BindPhoneActivity.this, Constant.isLogin, true);
        if(1==bean.getSex()){
            SPUtils.put(BindPhoneActivity.this,Constant.SEX,"男");
        }else{
            SPUtils.put(BindPhoneActivity.this,Constant.SEX,"女");
        }

        startActivity(new Intent(BindPhoneActivity.this, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timeCount!=null){
            timeCount.cancel();
            timeCount=null;
        }
    }
}
